package com.starsea.book.ui.newpage

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentHomeBinding
import com.starsea.book.databinding.LayoutHomeHeaderBannerBinding
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.main.MainFragmentInterface
import com.starsea.book.ui.newpage.adapter.HomeAdapter
import com.starsea.book.ui.newpage.entity.HomeBannerEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.ui.newpage.weidget.banner.Banner
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2023-12-26 09 GMT+8
 * @desc :
 */
class HomeFragment() : VMBaseFragment<HomeViewModel>(R.layout.fragment_home),
    MainFragmentInterface {

    private val binding by viewBinding(FragmentHomeBinding::bind)
    override val viewModel by viewModels<HomeViewModel>()

    lateinit var mAdapter: HomeAdapter
    var page = 1
    lateinit var headerBannerBinding: LayoutHomeHeaderBannerBinding

    constructor(position: Int) : this() {
        val bundle = Bundle()
        bundle.putInt("position", position)
        arguments = bundle
    }


    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

        mAdapter = HomeAdapter(requireContext(), 0) {

        }

        binding.rvContent.layoutManager = LinearLayoutManager(requireContext())
        binding.rvContent.adapter = mAdapter
        headerBannerBinding =
            LayoutHomeHeaderBannerBinding.inflate(layoutInflater, binding.rvContent, false)
        mAdapter.addHeaderView { headerBannerBinding }


        headerBannerBinding.banner.setAdapter(object : Banner.Adapter {
            override fun fillBannerItem(
                banner: Banner?,
                itemView: View,
                model: Any?,
                position: Int
            ) {
                (itemView as ImageView).loadNormalImage(
                    requireContext(),
                    (model as HomeBannerEntity).bannerUrl
                )
                itemView.setOnClickListener {
                    startActivity(
                        BookInfoActivity::class,
                        "bookId" to model.bookId
                    )
                }
            }

        })


        viewModel.onLinePersonResult.observe(this) {
            if (it.status == 200) {
                headerBannerBinding.tvUsePerson.text = "${it.data}人正在使用软件"
            }
        }

        binding.swRefresh.setOnRefreshListener {
            page = 1
            viewModel.getHomeData(page = page)
        }

        binding.swRefresh.setOnLoadMoreListener {
            page++
            viewModel.getHomeData(page = page)
        }
        viewModel.dataListResult.observe(this) {
            if (it.status == 200) {
                if (page == 1) {
                    mAdapter.setItems(it.data)
                    binding.swRefresh.finishRefresh()
                } else {
                    mAdapter.addItems(it.data!!)
                    binding.swRefresh.finishLoadMore()
                }
            }
        }
        viewModel.dataBannerResult.observe(this) {
            if (it.status == 200) {
                headerBannerBinding.banner.setData(it.data, null)
            }
        }


        viewModel.getHomeBannerData()
        viewModel.getHomeData(page = page)

        mAdapter.setOnItemClickListener { holder, item ->
            startActivity(
                BookInfoActivity::class,
                "bookId" to item.bookId
            )
        }
    }

    override val position: Int? get() = arguments?.getInt("position")

    override fun onResume() {
        super.onResume()
        viewModel.getOnlineNum()
    }

}